package com.iweb.day09;

import java.util.ArrayList;

/**
 * @author WuYongheng
 * @date 2021/11/15
 * @description 处理对象的方法，删除，先找到，再删除
 */
public class TestDelete1 {
    public static void main(String[] args){
        ArrayList personList = new ArrayList();

        // 处理对象
        personList.add(new Person("001","jerry"));
        personList.add(new Person("002","tom"));
        personList.add(new Person ("003","kitty"));
        // 删除tom 方法1： 先找到
        // 保持查找到的对象
        Person foundPerson = null;
        for (Object perObj : personList) {
            Person per= (Person) perObj;
            if ("tom".equals(per.getName())) {
                foundPerson = per;
                break;
            }
        }
        // 再删除
        if(foundPerson!=null){
            personList.remove(foundPerson);
        }
        // 遍历
        for (Object name : personList) {
            System.out.print(name + " ");
        }
    }
}
